var Hero = Mobs.extend({
	init: function(id, config, className){
		var classes = {
				hunter: {
					health: 100, mana: 100, damage: 8, healthRestore: 0.015, manaRestore: 0.015, speed: 6,
					spellSpecs: {dx: 15, dy: 20, manaCoast: 10, h: 32, w: 32, sprite: sprites.arrow, speed: 20, castDalay: 440, damege: 10}
				},
				mage: {
					health: 75, mana: 125, damage: 3, healthRestore: 0.010, manaRestore: 0.020, speed: 6,
					spellSpecs: {dx: 2, dy: 2, manaCoast: 7, h: 64, w: 64, sprite: sprites.fireball, speed: 13, castDalay: 300, damege: 20}
				},
				warrior: {
					health: 800, mana: 750, damage: 80, healthRestore: 0.020, manaRestore: 0.010, speed: 6,
					spellSpecs: {dx: 15, dy: 20, manaCoast: 13, h: 54, w: 54, sprite: sprites.spear, speed: 10, castDalay: 440, damege: 10}
				}	
			}
		this._super(id, config);
		this.isMoving = false;
		this.spellSpecs = classes[className].spellSpecs;
		this.mana = classes[className].mana;
		this.health = classes[className].health;
		this.manaRestore = classes[className].manaRestore;
		this.healthRestore = classes[className].healthRestore;
		this.maxHealth = this.health;
		this.maxMana = this.mana;
		this.speed = classes[className].speed;
		this.casts = {};
		this.equippedArmor = {};
		this.armor = 0;
		this.damage = classes[className].damage;
		this.numOfCasts = 0;
		this.isHitting = false;
		this.className = className;
		this.isAlive = true;
		this.questItems = [];
		this.takenQuests = [];
		this.returnedQuests = [];
		this.bossKilled = false;
		
		

		//fireball
		//spellSpecs: {dx: 0, dy: 10, manaCoast: 10, h: 64, w: 64, sprite: sprites.fireball, speed: 10, castDalay: 300}
		//spellSpecs: null
	},
	update: function(game){ //map, keysDown, bgObjects, quests, canvas, infoBar
		var bgObjects = game.backgroudnObjects[game.map.i][game.map.j]
		this.isHitting = false;
		this.isMoving = false;
		
		if(Math.floor(this.health) <= 0){
			this.kill();
			return;
		}

		this.restoreStats(this.mana, this.health);
		
		
		if (this.sprite.finite){     //if the action is finite (spell or hit), we only update its frames until it is done (the finite action).
			this.updateFrame();
			return;
		}
		
		if(game.map.isChanging){
			return;
		}
		
		for (var i = 0; i < game.quests.length; i++){
			if((this.isColliding(-this.speed, -this.speed, game.quests[i], null) || this.isColliding(this.speed, this.speed, game.quests[i], null)) && 
					(game.quests[i].i === game.map.i && game.quests[i].j === game.map.j)){
				game.quests[i].manage(game.infoBar, game.canvas);
			}else{
				game.quests[i].inRange = false;
				game.quests[i].isOpen = false;
			}
		}
		
		if (65 in game.keysDown) { //Player holding space				
			this.sprite = sprites.mele;
			this.frame = 0;
			this.updateFrame();
			this.isHitting = true;
			return;
			
		}
		
		if (83 in game.keysDown) { //Player holding shift		
			if (this.spellSpecs === null || this.mana - this.spellSpecs.manaCoast > 0){
				this.sprite = sprites.spell;
				this.frame = 0;
				this.updateFrame();
				if (!this.spellSpecs !== null)
					this.castSpell();
				return;
			}else{
				game.infoBar.setMessage('You dont\'t have enough mana!', game.canvas);
			}
			
		}
		
		if (38 in game.keysDown) { // Player holding up
			this.direction = 0;
			if (!game.map.isClear(this.x, this.y, this.direction)){
				return;
			}
			for (key in bgObjects){
				if(this.isColliding(0, -this.speed, bgObjects[key], bgObjects)){
					return;
				}
			}
			
			for (var i = 0; i < game.quests.length; i++){
				if(game.quests[i].i === game.map.i && game.quests[i].j === game.map.j 
						&& this.isColliding(0, -this.speed, game.quests[i], null)){
					return;
				}
			}
			
			this.y -= this.speed;
			this.isMoving = true;
			
		}
		if (40 in game.keysDown) { // Player holding down
			this.direction = 2;
			if(!game.map.isClear(this.x, this.y, this.direction)){
				return;
			}
			for (key in bgObjects){
				if(this.isColliding(0, this.speed, bgObjects[key], bgObjects)){
					return;
				}
			}
			
			for (var i = 0; i < game.quests.length; i++){
				if(game.quests[i].i === game.map.i && game.quests[i].j === game.map.j 
						&& this.isColliding(0, this.speed, game.quests[i], null)){
					return;
				}
			}
			
			this.y += this.speed;
			this.isMoving = true;
			
		}
		if (37 in game.keysDown) { // Player holding left
			this.direction = 1;
			if (!game.map.isClear(this.x, this.y, this.direction)){
				return;
			}
			
			for (key in bgObjects){
				if(this.isColliding(-this.speed, 0, bgObjects[key], bgObjects)){
					return;
				}
			}
			
			for (var i = 0; i < game.quests.length; i++){
				if(game.quests[i].i === game.map.i && game.quests[i].j === game.map.j 
						&& this.isColliding(-this.speed, 0, game.quests[i], null)){
					return;
				}
			}
			
			this.x -= this.speed;
			this.isMoving = true;
			
		}
		if (39 in game.keysDown) { // Player holding right
			this.direction = 3;
			if (!game.map.isClear(this.x, this.y, this.direction)){
				return;
			}
			for (key in bgObjects){
				if(this.isColliding(this.speed, 0, bgObjects[key], bgObjects)){
					return;
				}
			}
			
			for (var i = 0; i < game.quests.length; i++){
				if(game.quests[i].i === game.map.i && game.quests[i].j === game.map.j 
						&& this.isColliding(this.speed, 0, game.quests[i], null)){
					return;
				}
			}
			
			this.x += this.speed;
			this.isMoving = true;
			
		}
		
		if(this.isMoving){
			this.updateFrame();
		}
		
	},
	draw : function(canvas){
		this._super(canvas);
		for (i in this.equippedArmor){
			this.equippedArmor[i].update(this);
			if(!this.isAlive && i == 1 && this.className !== 'mage'){		//Ð°ÐºÐ¾ Ð³ÐµÑ€Ð¾Ñ� Ðµ ÑƒÐ¼Ñ€Ñ�Ð» Ð¼Ñƒ Ñ‡ÐµÑ€Ñ‚Ð°Ñ� ÑˆÐ°Ð¿ÐºÐ°Ñ‚Ð° Ð²Ñ�Ñ�ÐºÐ°Ñˆ Ðµ Ð¿Ð°Ð´Ð½Ð°Ð»Ð°
				this.equippedArmor[i].x -= 10;
				this.equippedArmor[i].y += 25;
			}
				this.equippedArmor[i].draw(canvas);
		}
	},
	castSpell: function(){
		
		var that = this;
		setTimeout(function (){
			that.casts[that.numOfCasts] = new Casts(that.numOfCasts++, {x: that.x + that.spellSpecs.dx, y: that.y + that.spellSpecs.dy, 
				width: that.spellSpecs.w, height: that.spellSpecs.h, sprite: that.spellSpecs.sprite, 
				direction: that.direction, speed: that.spellSpecs.speed, container: that.casts});
		},that.spellSpecs.castDalay);
		
		this.mana -= this.spellSpecs.manaCoast;	
	},
	
	hit : function(monster) {
		if(monster.detectHero(40,this)) {
			if(this.direction==0 && monster.y<this.y) {
				monster.health-=this.damage;
			}
			if(this.direction==1 && monster.x<this.x) {
				monster.health-=this.damage;
			}
			if(this.direction==2 && monster.y>this.y) {
				monster.health-=this.damage;
			}
			if(this.direction==3 && monster.x>this.x) {
				monster.health-=this.damage;
			}
		}
	},
	restoreStats: function(){
		if (this.mana < this.maxMana){
			this.mana += this.manaRestore;
			
		}
		if (this.health < this.maxHealth){
			this.health += this.healthRestore;
		}
	},
	equipArmor: function(specs){
		armorItem = new Armor(specs.id,{x: this.x, 
			y: this.y, width: this.w, height: this.h, direction: this.direction,
			frame: this.frame, speed: this.speed, container: this.equippedArmor,
			name: specs.name, armorPoints: 10});
		
		this.equippedArmor[armorItem.id] = armorItem;
		this.armor += armorItem.armorPoints;
	},
	kill: function(){
		this.sprite = sprites.hurt;
		this.direction = 0;
		this.frame = 5;
		this.isAlive = false;
	}
});